<template>
  <!-- 近七日出入库统计 -->
  <div id="WarehousecontentId" class="Warehousecontent" />
</template>

<script>
import { getSevenDayInOut } from "@/api/user";
import * as echarts from "echarts/core";
import {
  GridComponent,
  LegendComponent,
  TooltipComponent,
} from "echarts/components";
import { BarChart } from "echarts/charts";
import { CanvasRenderer } from "echarts/renderers";

echarts.use([
  GridComponent,
  LegendComponent,
  BarChart,
  CanvasRenderer,
  TooltipComponent,
]);
export default {
  name: "Warehouse",
  mounted() {
    this.getSevenDayInOut();
  },
  methods: {
    async getSevenDayInOut() {
      const res = await getSevenDayInOut();
      if (res.code === 200) {
        // console.log(res.data, "近七日出入库统计");
        const newArr = res.data.list.reverse();
        const ydata1 = []; // 入库
        const ydata2 = []; // 出库
        const xdata = []; // 日期
        newArr.forEach((item) => {
          ydata1.push(item.inNumber);
          ydata2.push(item.outNumber * -1);
          xdata.push(item.date.substring(5));
        });
        console.log(ydata1, ydata2, xdata, "近七日出入库统计");
        this.$nextTick(() => {
          this.initChart(ydata1, ydata2, xdata);
        });
      }
    },
    initChart(ydata1, ydata2, xdata) {
      const option = {
        tooltip: {
          show: true,
          trigger: "axis",
          formatter: (params) => {
            var tipString =
              params[0].seriesName +
              " : " +
              params[0].value +
              " <br/>" +
              params[1].seriesName +
              " : " +
              params[1].value * -1 +
              " <br/>";
            return tipString;
          },
        },
        legend: {
          right: 10,
          icon: "circle",
          textStyle: {
            color: "#fff",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        yAxis: [
          {
            splitLine: {
              lineStyle: {
                color: "rgba(255, 255, 255, 0.2)",
                width: 1,
              },
              show: true,
            },
            type: "value",
            axisLine: {
              show: true, // 是否显示坐标轴轴线，
              lineStyle: {
                color: "rgba(255, 255, 255, 0.2)", // x轴轴线的颜色
                width: 1, // x轴粗细
              },
            },
            axisTick: {
              show: false, // 是否显示刻度
              lineStyle: { color: "rgba(255,255,255,0.7)" }, // x轴刻度的颜色
            },
            axisLabel: {
              color: "rgba(255,255,255,0.7)", // x轴字体颜色
              fontSize: 8,
              interval: 0, // 轴显示所有的数据
            },
          },
        ],
        xAxis: [
          {
            axisLine: {
              lineStyle: {
                show: true, // 是否显示坐标轴轴线，
                color: "rgba(255,255,255,0.2)", // x轴轴线的颜色
                width: 1, // x轴粗细
              },
            },
            axisTick: {
              show: false, // 是否显示刻度
              lineStyle: { color: "#FFFFFF" }, // x轴刻度的颜色
            },
            axisLabel: {
              color: "rgba(255,255,255,0.7)", // x轴字体颜色
              interval: 0, // 轴显示所有的数据
            },
            type: "category",
            data: xdata,
          },
        ],
        series: [
          {
            barWidth: 12,
            name: "入库",
            type: "bar",
            stack: "Total",
            label: {
              show: false,
            },

            itemStyle: {
              // 使用方法二的写法
              borderRadius: [50, 50, 50, 50],
              color: {
                type: "linear",
                x: 0, // 右
                y: 0, // 下
                x2: 0, // 左
                y2: 1, // 上
                colorStops: [
                  {
                    offset: 0,
                    color: "#E5EA5F", // 0% 处的颜色
                  },
                  {
                    offset: 0.7,
                    color: "#A3E27D", // 70% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "#49DA91", // 100% 处的颜色
                  },
                ],
              },
            },
            data: ydata1,
          },
          {
            name: "出库",
            type: "bar",
            stack: "Total",
            label: {
              show: false,
            },
            itemStyle: {
              // 使用方法二的写法
              borderRadius: [50, 50, 50, 50],
              color: {
                type: "linear",
                x: 0, // 右
                y: 0, // 下
                x2: 0, // 左
                y2: 1, // 上
                colorStops: [
                  {
                    offset: 0,
                    color: "#3AE4DB", // 0% 处的颜色
                  },
                  {
                    offset: 0.7,
                    color: "#24BDEB", // 70% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "#1B9DF4", // 100% 处的颜色
                  },
                ],
              },
            },
            data: ydata2,
          },
        ],
      };
      var chartDom = document.getElementById("WarehousecontentId");
      var myChart = echarts.init(chartDom);
      option && myChart.setOption(option);
    },
  },
};
</script>

<style scoped lang="scss">
.Warehousecontent {
  color: aliceblue;
  width: 100%;
  height: 100%;
  padding: 0.1563rem 0.0781rem 0.0521rem;
}
</style>
